package com.zoho.daliansheng.task;

import com.zoho.daliansheng.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Configuration
@EnableScheduling
public class ScheduleTask {

    ProductService productService;

    WarehouseService warehouseService;

    UserService userService;

    CurrentStockService currentStockService;

    CurrentStockDateService dateService;

    AcceptListService acceptlistService;

    AcceptDetailListService acceptDetailListService;

    AcceptListBusinessService acceptListBusinessService;

    FaHuoService faHuoService;

    CustomerAuthService customerAuthService;

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public ScheduleTask(ProductService productService, WarehouseService warehouseService, UserService userService, CurrentStockService currentstockService, AcceptListService acceptlistService, CurrentStockDateService dateService, AcceptDetailListService acceptDetialListService, AcceptListBusinessService acceptListBusinessService, CustomerAuthService customerAuthService, FaHuoService faHuoService) {
        this.productService = productService;
        this.warehouseService = warehouseService;
        this.userService = userService;
        this.currentStockService = currentstockService;
        this.acceptlistService = acceptlistService;
        this.dateService = dateService;
        this.acceptDetailListService = acceptDetialListService;
        this.acceptListBusinessService = acceptListBusinessService;
        this.customerAuthService = customerAuthService;
        this.faHuoService = faHuoService;
    }

    /**
     * 同步ERP的存货档案，到CRM产品模块,每天20点同步
     */
    @Scheduled(cron = "0 0 22 ? * *")
    //@Scheduled(cron = "0/5 * * * * ?")
    public void updateCustomerData() {
        log.info("-----开始【CRM产品模块】定时任务 " + format.format(new Date()) + "-----");
        productService.synProduct();
        log.info("-----结束【CRM产品模块】定时任务 " + format.format(new Date()) + "-----");
    }

    /**
     * 同步ERP的仓库，到CRM产品仓库模块，每天21点同步
     */
    @Scheduled(cron = "0 0 21 ? * *")
    //@Scheduled(cron = "0/5 * * * * ?")
    public void updateWarehouseData() {
        log.info("-----开始【CRM产品仓库模块】定时任务 " + format.format(new Date()) + "-----");
        warehouseService.synWarehouse();
        log.info("-----结束【CRM产品仓库模块】定时任务 " + format.format(new Date()) + "-----");
    }

    /**
     * 同步ERP的人员，到CRM 用户，每周日07:00同步
     */
    @Scheduled(cron = "0 0 7 * * 0")
    //@Scheduled(cron = "0/5 * * * * ?")
    public void updateUserData() {
        log.info("-----开始【CRM用户】定时任务 " + format.format(new Date()) + "-----");
        userService.synUser();
        log.info("-----结束【CRM用户】定时任务 " + format.format(new Date()) + "-----");
    }

    /**
     * 同步ERP的现存量，到CRM 现存量 模块，每天8,12,14,16,18点
     */
    @Scheduled(cron = "0 0 8,12,14,16,18 ? * *")
    //@Scheduled(cron = "0/5 * * * * ?")
    public void updateCurrentStockData() {
        log.info("-----开始【CRM现存量模块】定时任务 " + format.format(new Date()) + "-----");
        currentStockService.synCurrentStock();
        log.info("-----结束【CRM现存量模块】定时任务 " + format.format(new Date()) + "-----");
        log.info("-----开始【CRM现存量入库日期模块】定时任务 " + format.format(new Date()) + "-----");
        dateService.synCurrentStockDate();
        log.info("-----结束【CRM现存量入库日期模块】定时任务 " + format.format(new Date()) + "-----");
    }

    /**
     * 同步ERP的 管理维度，到CRM 客户管理维度 模块，每周日05:00同步
     */
    @Scheduled(cron = "0 0 5 * * 0")
    //@Scheduled(cron = "0/5 * * * * ?")
    public void updateCustomerAuthData() {
        log.info("-----开始【CRM客户管理维度模块】定时任务 " + format.format(new Date()) + "-----");
        customerAuthService.sysCustomerAuth();
        log.info("-----结束【CRM客户管理维度模块】定时任务 " + format.format(new Date()) + "-----");
    }

    /**
     * 同步ERP的 发货单，到CRM 销售发货单 模块，每天3点30、10点30、12点30、15点30、17点30、
     */
    @Scheduled(cron = "0 30 3,10,12,15,17 ? * *")
    //@Scheduled(cron = "0/5 * * * * ?")
    public void updateDispatchListData() {
        log.info("-----开始【CRM发货单】定时任务 " + format.format(new Date()) + "-----");
        faHuoService.synFaHuoList();
        log.info("-----结束【CRM发货单】定时任务 " + format.format(new Date()) + "-----");
    }

    /**
     * 同步ERP的 收款单，到CRM 收款单 模块，每天1点50、10点50、12点50、15点50、17点50、
     */
    @Scheduled(cron = "0 50 1,10,12,15,17 ? * *")
    //@Scheduled(cron = "0/5 * * * * ?")
    public void updateAcceptListData() {
        log.info("-----开始【CRM收款单模块】定时任务 " + format.format(new Date()) + "-----");
        acceptlistService.synAcceptList();
        log.info("-----结束【CRM收款单模块】定时任务 " + format.format(new Date()) + "-----");
        log.info("-----开始【CRM收款单商业汇票模块】定时任务 " + format.format(new Date()) + "-----");
        acceptListBusinessService.synAcceptBusinessList();
        log.info("-----结束【CRM收款单商业汇票模块】定时任务 " + format.format(new Date()) + "-----");
    }
}
